home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / include / AudioEngine.h next >
Encoding:
C/C++ Source or Header  |  2005-11-17  |  6.7 KB  |  247 lines

  1.  
  2. #ifndef __AUDIOENGINE_H_
  3. #define __AUDIOENGINE_H_
  4. /*
  5. Peon - Win32 Games Programming Library
  6. Copyright (C) 2002-2005 Erik Yuzwa
  7.  
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public
  10. License as published by the Free Software Foundation; either
  11. version 2 of the License, or (at your option) any later version.
  12.  
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. Library General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with this library; if not, write to the Free
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  
  22. Erik Yuzwa
  23. peon AT wazooinc DOT com
  24. */
  25.  
  26. #include "ISingleton.h"
  27.  
  28.  
  29. #define NUM_BUFFERS 30
  30.  
  31. #define OGG_BUFFER_SIZE (4096 * 8)
  32.  
  33. namespace peon
  34. {
  35.     /**
  36.     * This structure is responsible for encapsulating a 3D sound
  37.     * within our game world. Hopefully it's fairly generic enough
  38.     * to handle most situations. These nodes are really only meant
  39.     * for OGG and WAV files (pretty much anything supported by OpenAL).
  40.     * For MIDI audio, you should be looking at the loadMidi method
  41.     * of the AudioEngine object.
  42.     */
  43.     struct PEONMAIN_API AudioNode
  44.     {
  45.         /** the source buffer */
  46.         ALuint sound_source;
  47.  
  48.         int sound_buffer;
  49.  
  50.         /** loop the sound? */
  51.         bool sound_loop;
  52.  
  53.         /** sound's position in 3D space */
  54.         ALfloat sound_position[3];
  55.  
  56.         /** sound's velocity within the game world */
  57.         ALfloat sound_velocity[3];
  58.  
  59.     public:
  60.         /**
  61.         * Constructor
  62.         */
  63.         AudioNode()
  64.         {
  65.             sound_source = -1;
  66.             sound_buffer = -1;
  67.             sound_loop = false;
  68.             sound_position[0] = 0.0f;
  69.             sound_position[1] = 0.0f;
  70.             sound_position[2] = 0.0f;
  71.  
  72.             sound_velocity[0] = 0.0f;
  73.             sound_velocity[1] = 0.0f;
  74.             sound_velocity[2] = 0.0f;
  75.         }
  76.         
  77.         
  78.     };
  79.  
  80.  
  81.     /**
  82.     * This object is used to encapsulate our audio hardware. It should
  83.     * be able to play both MIDI tunes (yes people still use 'em) and 
  84.     * OGG/WAV files.
  85.     *
  86.     */ 
  87.     class PEONMAIN_API AudioEngine : public ISingleton<AudioEngine>
  88.     {
  89.  
  90.     protected:
  91.         /** The OpenAL context */
  92.         ALCcontext *m_pALContext;
  93.         
  94.         /** The OpenAL device */
  95.         ALCdevice  *m_pALDevice;
  96.  
  97.         /** do we enable/disable sound? Note this only affects OGG and WAV playback */
  98.         bool        m_bEnableSound;
  99.         
  100.         /** do we enable/disable music? Note this only affects MIDI playback */
  101.         bool        m_bEnableMusic;
  102.  
  103.         /** a storage buffer for NUM_BUFFERS number of sounds */
  104.         ALuint      m_uAudioBuffers[ NUM_BUFFERS ];
  105.  
  106.         /** is our audio hardware even supported? */
  107.         bool        m_bAudioSupported;
  108.  
  109.         int            m_iCurrentSlot;
  110.         
  111.  
  112.     public:
  113.         /**
  114.         * Constructor
  115.         */
  116.         AudioEngine();
  117.  
  118.         /**
  119.         * Destructor
  120.         */
  121.         ~AudioEngine();
  122.  
  123.         /** Override standard Singleton retrieval.
  124.         @remarks
  125.         Why do we do this? Well, it's because the Singleton
  126.         implementation is in a .h file, which means it gets compiled
  127.         into anybody who includes it. This is needed for the
  128.         Singleton template to work, but we actually only want it
  129.         compiled into the implementation of the class based on the
  130.         Singleton, not all of them. If we don't change this, we get
  131.         link errors when trying to use the Singleton-based class from
  132.         an outside dll.
  133.         @par
  134.         This method just delegates to the template version anyway,
  135.         but the implementation stays in this single compilation unit,
  136.         preventing link errors.
  137.         */
  138.         static AudioEngine& getSingleton(void);
  139.         /** Override standard Singleton retrieval.
  140.         @remarks
  141.         Why do we do this? Well, it's because the Singleton
  142.         implementation is in a .h file, which means it gets compiled
  143.         into anybody who includes it. This is needed for the
  144.         Singleton template to work, but we actually only want it
  145.         compiled into the implementation of the class based on the
  146.         Singleton, not all of them. If we don't change this, we get
  147.         link errors when trying to use the Singleton-based class from
  148.         an outside dll.
  149.         @par
  150.         This method just delegates to the template version anyway,
  151.         but the implementation stays in this single compilation unit,
  152.         preventing link errors.
  153.         */
  154.         static AudioEngine* getSingletonPtr(void);
  155.  
  156.  
  157.         /**
  158.         * This method loads and instantiates the subsystems necessary in
  159.         * SDL_Mixer and OpenAL to get a device working for each. If the
  160.         * initialization fails, then you can decide if you want to just
  161.         * disable all sound, or quit the app entirely.
  162.         * @param pConfig - The INI information
  163.         * @return bool - true if audio loaded properly
  164.         */
  165.         bool loadEngine( IniConfigReader* pConfig );
  166.  
  167.         /**
  168.         * This method frees up our allocated audio resources
  169.         */
  170.         void unloadEngine();
  171.  
  172.         /**
  173.         * This method makes the necessary calls to load up a 
  174.         * Mix_Music instance which is used for playback of
  175.         * MIDI files
  176.         * @param strFilename - path to the MIDI file
  177.         * @return Mix_Music* - pointer to our Mix_Music object
  178.         */
  179.         Mix_Music* loadMIDI( const String& strFilename );
  180.  
  181.         /**
  182.         * This method makes the necessary calls to load up a 
  183.         * Mix_Chunk instance which is used for playback of
  184.         * MIDI files
  185.         * @param strFilename - path to the WAV file
  186.         * @return Mix_Music* - pointer to our Mix_Chunk object
  187.         */
  188.         Mix_Chunk* loadWAVChunk( const String& strFilename );
  189.  
  190.  
  191.         /**
  192.         * This method internally loads the audio resource
  193.         * into some OpenAL compatible buffers. When you wish
  194.         * to work with a resource, you need to reference it by
  195.         * the slot you stored it in.
  196.         * @param strFilename - path to WAV file
  197.         * @param slot - slot to store resource
  198.         * @return bool - true if sound loaded properly
  199.         */
  200.         bool loadAudioNode( const String& strWAVFile, AudioNode* pNode );
  201.  
  202.         /**
  203.         * This method is responsible for setting the current
  204.         * AudioNode
  205.         * @param pNode - pointer to valid AudioNode
  206.         */
  207.         void setAudioNode( AudioNode* pNode );
  208.  
  209.         /**
  210.         * This method is responsible for playing the currently
  211.         * set AudioNode
  212.         * @param pNode - pointer to a valid AudioNode
  213.         */
  214.         void playAudioNode( AudioNode* pNode );
  215.  
  216.         /**
  217.         * This method is responsible for stopping the playback
  218.         * of the AudioNode
  219.         * @param pNode - pointer to AudioNode to stop
  220.         */
  221.         void stopAudioNode( AudioNode* pNode );
  222.  
  223.         /**
  224.         * This method just enables the global audio mask
  225.         */
  226.         void enableSound(){ m_bEnableSound = true; }
  227.  
  228.         /**
  229.         * This method disables the global audio mask
  230.         */
  231.         void disableSound(){ m_bEnableSound = false; }
  232.  
  233.         /**
  234.         * This method enables the global music mask
  235.         */
  236.         void enableMusic(){ m_bEnableMusic = true; }
  237.  
  238.         /**
  239.         * This method disables the global music mask
  240.         */
  241.         void disableMusic(){ m_bEnableMusic = false; }
  242.  
  243.     };
  244. }
  245.  
  246. #endif
  247.